//
//  LXEditViewController.m
//  cat
//
//  Created by imera on 16/3/14.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXEditViewController.h"
#import "LXNavBarView.h"
#import "LXUserInfoEditView.h"
#import "LXSelectView.h"
#import "LXUserNameEditViewController.h"
#import "LXEditUserSignViewController.h"
#import "LXChangeUserPwdViewController.h"
#import "MBProgressHUD+MJ.h"
#import "AFNetworking.h"
#import "MBProgressHUD+MJ.h"

@interface LXEditViewController() <LXNavBarViewDelegate, LXSelectViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPickerViewDataSource, UIPickerViewDelegate>
/** 灰色背景 */
@property (nonatomic, weak) UIView *bgGrayView;
/** 用户头像选择 */
@property (nonatomic, weak) LXSelectView *photoSelectView;
/** 用户性别选择 */
@property (nonatomic, weak) LXSelectView *sexSelectView;
/** 用户头像 */
@property (nonatomic, weak) LXUserInfoEditView *userPhoto;
/** 用户昵称 */
@property (nonatomic, weak) LXUserInfoEditView *userName;
/** 用户性别 */
@property (nonatomic, weak) LXUserInfoEditView *userSex;
/** 用户生日 */
@property (nonatomic, weak) LXUserInfoEditView *userBirthday;
/** 用户的性别 */
@property (nonatomic, assign) int sex;
/** 日期选择器 */
@property (nonatomic, weak) UIPickerView *datePicker;

@property (nonatomic, weak) UIView *birthDayView;
@end

@implementation LXEditViewController
{
    
    NSMutableArray *yearArray;
    NSArray *monthArray;
    NSMutableArray *monthMutableArray;
    NSMutableArray *DaysMutableArray;
    NSMutableArray *DaysArray;
    NSString *currentMonthString;
    
    NSInteger selectedYearRow;
    NSInteger selectedMonthRow;
    NSInteger selectedDayRow;
    
    BOOL firstTimeLoad;
    
    NSInteger m ;
    int year;
    int month;
    int day;
    
}

- (void)setUser:(LXUser *)user
{
    if (user != nil) {
        _user = user;
    }
    [self initSubViews];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = MainBackgroundColor;
    [self initSubViews];
    
     [self addObserver];
}

#pragma mark - 注册通知
- (void)addObserver
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(userNameChange) name:@"userNameChange" object:nil];
}

#pragma mark - 用户昵称被修改
- (void)userNameChange
{
    self.userName.titleContent = self.user.uname;
}

#pragma mark -  初始化子控件
- (void)initSubViews
{
    LXNavBarView *navBarView = [[LXNavBarView alloc]init];
    navBarView.frame = CGRectMake(0, 20, self.view.frame.size.width, LXNavBarHeight);
    navBarView.leftTitle = @"返回";
    navBarView.myDelegate = self;
    [self.view addSubview:navBarView];

    /** 头像 */
    LXUserInfoEditView *userPhoto = [LXUserInfoEditView initWithTitle:@"头像" image:self.user.headpic];
    userPhoto.isPhoto = YES;
    userPhoto.isDivder = NO;
    [self.view addSubview:userPhoto];
    self.userPhoto = userPhoto;
    
    CGFloat userPhotoX = 5;
    CGFloat userPhotoY = 70;
    CGFloat userPhotoW = self.view.frame.size.width - userPhotoX * 2;
    CGFloat userPhotoH = 50;
    userPhoto.frame = CGRectMake(userPhotoX, userPhotoY, userPhotoW, userPhotoH);
    
    //添加点击事件
    UITapGestureRecognizer *userPhotoTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editUserPhoto)];
    [userPhoto addGestureRecognizer:userPhotoTap];
    
    /** 昵称 */
    LXUserInfoEditView *userName = [LXUserInfoEditView initWithTitle:@"昵称" titleContent:self.user.uname];
    self.userName = userName;
    [self.view addSubview:userName];
    
    CGFloat userNameX = userPhotoX;
    CGFloat userNameY = CGRectGetMaxY(userPhoto.frame) + 10 ;
    CGFloat userNameW = userPhotoW;
    CGFloat userNameH = 45;
    userName.frame = CGRectMake(userNameX, userNameY, userNameW, userNameH);
    
    UITapGestureRecognizer *userNameTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userNameTap)];
    [userName addGestureRecognizer:userNameTap];
    
    /** 性别 */
    NSString *sex = self.user.sex == 0 ? @"男":@"女";
    self.sex = (int)self.user.sex;
    LXUserInfoEditView *userSex = [LXUserInfoEditView initWithTitle:@"性别" titleContent:sex];
    [self.view addSubview:userSex];
    self.userSex = userSex;

    CGFloat userSexX = userPhotoX;
    CGFloat userSexY = CGRectGetMaxY(userName.frame);
    CGFloat userSexW = userPhotoW;
    CGFloat userSexH = userNameH;
    userSex.frame = CGRectMake(userSexX, userSexY, userSexW, userSexH);
    
    UITapGestureRecognizer *userSexTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(userSexTap)];
    [userSex addGestureRecognizer:userSexTap];
    
    /** 生日 */
    LXUserInfoEditView *userBirthday = [LXUserInfoEditView initWithTitle:@"生日" titleContent:self.user.birth];
    self.userBirthday = userBirthday;
    [self.view addSubview:userBirthday];
    
    CGFloat userBirthdayX = userPhotoX;
    CGFloat userBirthdayY = CGRectGetMaxY(userSex.frame);
    CGFloat userBirthdayW = userPhotoW;
    CGFloat userBirthdayH = userNameH;
    userBirthday.frame = CGRectMake(userBirthdayX, userBirthdayY, userBirthdayW, userBirthdayH);
    
    UITapGestureRecognizer *userBirthdayTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editUserBirth)];
    [userBirthday addGestureRecognizer:userBirthdayTap];
    
    /** 个性签名 */
    LXUserInfoEditView *userSign = [LXUserInfoEditView initWithTitle:@"个性签名" titleContent:@""];
    [self.view addSubview:userSign];
    
    CGFloat userSignX = userPhotoX;
    CGFloat userSignY = CGRectGetMaxY(userBirthday.frame) + 10;
    CGFloat userSignW = userPhotoW;
    CGFloat userSignH = userNameH;
    userSign.frame = CGRectMake(userSignX, userSignY, userSignW, userSignH);
    
    UITapGestureRecognizer *userSignTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editUserSign)];
    [userSign addGestureRecognizer:userSignTap];
    
    /** 账号安全中心 */
    LXUserInfoEditView *userPwd = [LXUserInfoEditView initWithTitle:@"账号安全中心" titleContent:@""];
    [self.view addSubview:userPwd];
    
    CGFloat userPwdX = userPhotoX;
    CGFloat userPwdY = CGRectGetMaxY(userSign.frame) + 10;
    CGFloat userPwdW = userPhotoW;
    CGFloat userPwdH = userNameH;
    userPwd.frame = CGRectMake(userPwdX, userPwdY, userPwdW, userPwdH);
    
    UITapGestureRecognizer *userPwdTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeUserPwd)];
    [userPwd addGestureRecognizer:userPwdTap];
    
    /** 退出登录 */
    UIButton *quitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    quitBtn.backgroundColor = [UIColor whiteColor];
    quitBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [quitBtn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [quitBtn setTitle:@"退出登录" forState:UIControlStateNormal];
    [quitBtn addTarget:self action:@selector(quit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:quitBtn];
    
    CGFloat quitBtnX = userPhotoX;
    CGFloat quitBtnY = CGRectGetMaxY(userPwd.frame) + 10;
    CGFloat quitBtnW = userPhotoW;
    CGFloat quitBtnH = userNameH;
    quitBtn.frame = CGRectMake(quitBtnX, quitBtnY, quitBtnW, quitBtnH);
    
    /** 灰色背景 */
    UIView *bgGrayView = [[UIView alloc]init];
    bgGrayView.backgroundColor = [UIColor grayColor];
    bgGrayView.alpha = 0;
    bgGrayView.frame = CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height - 20);
    [self.view addSubview:bgGrayView];
    self.bgGrayView = bgGrayView;
    
    UITapGestureRecognizer *bgGrayViewTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bgGrayViewTap)];
    [bgGrayView addGestureRecognizer:bgGrayViewTap];
    
    /** 日期选择器 */
    UIView *birthDayView = [[UIView alloc]init];
    self.birthDayView = birthDayView;
    self.birthDayView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:birthDayView];
    
    CGFloat birthDayViewX = 0;
    CGFloat birthDayViewW = self.view.frame.size.width;
    CGFloat birthDayViewH = 230;
    CGFloat birthDayViewY = self.view.frame.size.height;
    birthDayView.frame = CGRectMake(birthDayViewX, birthDayViewY, birthDayViewW, birthDayViewH);
    
    /** 日期选择器取消按钮 */
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(birthCancel) forControlEvents:UIControlEventTouchUpInside];
    [birthDayView addSubview:cancelBtn];
    
    CGFloat cancelBtnW = 70;
    CGFloat cancelBtnH = 30;
    CGFloat cancelBtnX = 0;
    CGFloat cancelBtnY = 0;
    cancelBtn.frame = CGRectMake(cancelBtnX, cancelBtnY, cancelBtnW, cancelBtnH);

    /** 日期选择器确定按钮 */
    UIButton *enSureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [enSureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [enSureBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [enSureBtn addTarget:self action:@selector(selectBirthday) forControlEvents:UIControlEventTouchUpInside];
    [birthDayView addSubview:enSureBtn];
    
    CGFloat enSureBtnW = cancelBtnW;
    CGFloat enSureBtnH = cancelBtnH;
    CGFloat enSureBtnX = birthDayView.frame.size.width - enSureBtnW;
    CGFloat enSureBtnY = 0;
    enSureBtn.frame  = CGRectMake(enSureBtnX, enSureBtnY, enSureBtnW, enSureBtnH);
    
    /** 日期选择器 */
    UIPickerView *datePicker = [[UIPickerView alloc]init];
    datePicker.delegate = self;
    [birthDayView insertSubview:datePicker atIndex:self.view.subviews.count];
    self.datePicker = datePicker;

    CGFloat datePickerX = 0;
    CGFloat datePickerY = cancelBtnH ;
    CGFloat datePickerW = birthDayView.frame.size.width;
    CGFloat datePickerH = birthDayView.frame.size.height - datePickerY;
    datePicker.frame = CGRectMake(datePickerX, datePickerY, datePickerW, datePickerH);
    
    [self initDate];
}

#pragma mark - 取消选择日期
- (void)birthCancel
{
    [self quitSelectBirth];
}

#pragma mark - 选择日期
- (void)selectBirthday
{
    [self quitSelectBirth];
    
    // 1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    NSString *birthDay = [NSString stringWithFormat:@"%@/%@/%@ ",[yearArray objectAtIndex:[self.datePicker selectedRowInComponent:0]],[monthArray objectAtIndex:[self.datePicker selectedRowInComponent:1]],[DaysArray objectAtIndex:[self.datePicker selectedRowInComponent:2]]];
    // 2.发送请求
    [mgr POST:@"http://cat666.com/cat666-interface/index.php/index/alterBirth" parameters:@{@"userid":@(self.user.ID), @"birth":birthDay} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = responseObject;
        if([dict[@"successed"] integerValue] == 1){
            [MBProgressHUD showSuccess:@"修改生日成功"];
            self.userBirthday.titleContent = birthDay;
            
        }else{
            [MBProgressHUD showSuccess:@"修改生日失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showSuccess:@"修改生日失败"];
    }];
    
}

#pragma mark - 退出选择生日
- (void)quitSelectBirth
{
    self.bgGrayView.alpha = 0;
    
    UIView *birthDayView = self.birthDayView;
    CGRect frame = birthDayView.frame;
    frame.origin.y = self.view.frame.size.height;
    [UIView animateWithDuration:0.5 animations:^{
        birthDayView.frame = frame;
    }];
}

#pragma mark - 初始化日期数据
- (void)initDate
{
    m=0;
    firstTimeLoad = YES;
    NSDate *date = [NSDate date];
    
    // Get Current Year
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy"];
    
    NSString *currentyearString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    year =[currentyearString intValue];
    
    // Get Current  Month
    [formatter setDateFormat:@"MM"];
    
    currentMonthString = [NSString stringWithFormat:@"%ld",(long)[[formatter stringFromDate:date]integerValue]];
    month=[currentMonthString intValue];
    
    // Get Current  Date
    [formatter setDateFormat:@"dd"];
    NSString *currentDateString = [NSString stringWithFormat:@"%@",[formatter stringFromDate:date]];
    
    day =[currentDateString intValue];
    
    yearArray = [[NSMutableArray alloc]init];
    monthMutableArray = [[NSMutableArray alloc]init];
    DaysMutableArray= [[NSMutableArray alloc]init];
    for (int i = 1970; i <= year ; i++){
        [yearArray addObject:[NSString stringWithFormat:@"%d",i]];
    }

    // PickerView -  Months data
    monthArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12"];
    for (int i=1; i<month+1; i++) {
        [monthMutableArray addObject:[NSString stringWithFormat:@"%d",i]];
    }
    DaysArray = [[NSMutableArray alloc]init];
    
    for (int i = 1; i <= 31; i++){
        [DaysArray addObject:[NSString stringWithFormat:@"%d",i]];
        
    }
    for (int i = 1; i <day+1; i++){
        [DaysMutableArray addObject:[NSString stringWithFormat:@"%d",i]];
        
    }

    // 设置初始默认值
    [self.datePicker selectRow:20 inComponent:0 animated:YES];
    [self.datePicker selectRow:[monthArray indexOfObject:currentMonthString] inComponent:1 animated:YES];
    [self.datePicker selectRow:0 inComponent:2 animated:YES];
}

#pragma mark - 退出头像选择
- (void)bgGrayViewTap
{
    self.bgGrayView.alpha = 0;
    
    if (self.photoSelectView != nil) {
        [self.photoSelectView removeFromSuperview];
    }
    
    if (self.sexSelectView != nil) {
        [self.sexSelectView removeFromSuperview];
    }
    
    [self quitSelectBirth];
}

#pragma mark - 编辑昵称
- (void)userNameTap
{
    LXUserNameEditViewController *userNameEditVc = [[LXUserNameEditViewController alloc]init];
    userNameEditVc.user = self.user;
    [self.navigationController pushViewController:userNameEditVc animated:YES];
}

#pragma mark - 编辑性别
- (void)userSexTap
{
    self.bgGrayView.alpha = 0.2;
    
    LXSelectView *sexSelectView = [[LXSelectView alloc]initWithTitle:@"性别" imageArr:@[@"man",@"women"] imageTitleArr:@[@"男",@"女"] bottomBtnTitle:@"确定"];
    [self.view addSubview:sexSelectView];
    self.sexSelectView = sexSelectView;
    sexSelectView.MyDelegate = self;
    sexSelectView.tag = 1;
    sexSelectView.isSelectItemCircle = YES;

    CGFloat sexSelectViewW = 280;
    CGFloat sexSelectViewH = 210;
    CGFloat sexSelectViewX = (self.view.frame.size.width - sexSelectViewW) / 2;
    CGFloat sexSelectViewY = (self.view.frame.size.height - sexSelectViewH) / 2;
    sexSelectView.frame = CGRectMake(sexSelectViewX, sexSelectViewY, sexSelectViewW, sexSelectViewH);
    
    [sexSelectView btnSelected:self.sex];
}

#pragma mark - 编辑生日
- (void)editUserBirth
{
    self.bgGrayView.alpha = 0.2;
    
    UIView *birthDayView = self.birthDayView;
    CGRect frame = birthDayView.frame;
    frame.origin.y = self.view.frame.size.height - frame.size.height;
    [UIView animateWithDuration:0.5 animations:^{
        birthDayView.frame = frame;
    }];
}

#pragma mark - 编辑用户头像
- (void)editUserPhoto
{
    self.bgGrayView.alpha = 0.2;
    
    LXSelectView *photoSelectView = [[LXSelectView alloc]initWithTitle:@"头像选择" imageArr:@[@"camera",@"picture"] imageTitleArr:@[@"拍照",@"图片库"] bottomBtnTitle:@"取消"];
    [self.view addSubview:photoSelectView];
    self.photoSelectView = photoSelectView;
    photoSelectView.MyDelegate = self;
    photoSelectView.tag = 0;
    
    CGFloat photoSelectViewW = 280;
    CGFloat photoSelectViewH = 210;
    CGFloat photoSelectViewX = (self.view.frame.size.width - photoSelectViewW) / 2;
    CGFloat photoSelectViewY = (self.view.frame.size.height - photoSelectViewH) / 2;
    photoSelectView.frame = CGRectMake(photoSelectViewX, photoSelectViewY, photoSelectViewW, photoSelectViewH);
}

#pragma mark - 编辑个性签名
- (void)editUserSign
{
    LXEditUserSignViewController *editUserSignVc = [[LXEditUserSignViewController alloc]init];
    editUserSignVc.user = self.user;
    [self.navigationController pushViewController:editUserSignVc animated:YES];
}

#pragma mark - 修改用户密码
- (void)changeUserPwd
{
    LXChangeUserPwdViewController *changeUserPwdVc = [[LXChangeUserPwdViewController alloc]init];
    changeUserPwdVc.user = self.user;
    [self.navigationController pushViewController:changeUserPwdVc animated:YES];
}

#pragma mark - 退出登录
- (void)quit
{
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"userName"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"userId"];
    [[NSUserDefaults standardUserDefaults]removeObjectForKey:@"password"];
    
    [MBProgressHUD showSuccess:@"注销成功"];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 导航条代理
- (void)popCurrentViewController:(LXNavBarView *)navBarView
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 编辑视图代理
- (void)bottomBtnClick:(LXSelectView *)selectView
{
    if (selectView.tag == 0) {
        [self.photoSelectView removeFromSuperview];
    }
    
    if (selectView.tag == 1) {
        [self.sexSelectView removeFromSuperview];
        
        //性别选择
        [self selectSex:self.sex];
    }
    
    self.bgGrayView.alpha = 0;
}

- (void)itemSelected:(LXSelectView *)selectView tag:(int)tag
{
    if (selectView.tag == 0) {
        switch (tag) {
            case 0: //拍照
                [self camera];
                break;
            case 1:  //图片库
                [self picture];
                break;
            default:
                break;
        }
    }

    if (selectView.tag == 1) {
        self.sex = tag;
    }
}

#pragma mark - 拍照上传图片
- (void)camera
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing = YES;
    
    [self presentViewController:imagePicker animated:YES completion:nil];
}

#pragma mark - 从图片库选择图片上传
- (void)picture
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing = YES;
    
    [self presentViewController:imagePicker animated:YES completion:nil];
}
    
#pragma mark - 选择性别
- (void)selectSex:(int)sex
{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    
    [mgr POST:@"http://cat666.com/cat666-interface/index.php/index/alterSex" parameters:@{@"userid":@(self.user.ID), @"sex":@(sex)} success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = responseObject;
        if([dict[@"successed"] integerValue] == 1){
            
            [MBProgressHUD showSuccess:@"修改性别成功"];
            
            self.user.sex = sex;
            self.userSex.titleContent = sex == 0 ? @"男":@"女";
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"userSexChange" object:nil];
            
        }else{
            [MBProgressHUD showSuccess:@"修改性别失败"];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD showSuccess:@"修改性别失败"];
    }];
    
    self.bgGrayView.alpha = 0;
    if (self.sexSelectView != nil) {
        [self.sexSelectView removeFromSuperview];
    }
    
}

#pragma mark - imagePicker结束回调方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    self.bgGrayView.alpha = 0;
    if (self.photoSelectView != nil) {
        [self.photoSelectView removeFromSuperview];
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];

    UIImage *image= [info objectForKey:@"UIImagePickerControllerEditedImage"];
    NSData *imageData = UIImageJPEGRepresentation(image, 1);

    // 1.创建请求管理对象
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    //2.发送请求
    [mgr POST:@"http://cat666.com/cat666-interface/index.php/index/alterPic" parameters:@{@"userid":@(self.user.ID)} constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imageData name:@"pic" fileName:@"userPhoto" mimeType:@"image/jpeg"];

    } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *dict = responseObject;
        if ([dict.allKeys containsObject:@"failed"] || [dict.allKeys containsObject:@"error"]) {
            [MBProgressHUD showError:@"上传头像失败"];
        }
        if([dict.allKeys containsObject:@"successed"]){
            [MBProgressHUD showSuccess:@"上传头像成功"];
            
            self.user.headpic = [NSString stringWithFormat:@"http://www.cat666.com%@",dict[@"pic"]];
            self.userPhoto.image = self.user.headpic;
            [[NSNotificationCenter defaultCenter]postNotificationName:@"userHeadPicChange" object:nil];
        }
        
    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        [MBProgressHUD showError:@"您的网络开小差了..."];
    }];
    
}

#pragma mark - pickerView 代理和数据源方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    m=row;
    
    if (component == 0)
    {
        selectedYearRow = row;
        [self.datePicker reloadAllComponents];
    }
    else if (component == 1)
    {
        selectedMonthRow = row;
        [self.datePicker reloadAllComponents];
    }
    else if (component == 2)
    {
        selectedDayRow = row;
        [self.datePicker reloadAllComponents];
        
    }
    
}

#pragma mark - UIPickerViewDatasource
- (UIView *)pickerView:(UIPickerView *)pickerView
            viewForRow:(NSInteger)row
          forComponent:(NSInteger)component
           reusingView:(UIView *)view {
    
    // Custom View created for each component
    UILabel *pickerLabel = (UILabel *)view;
    
    if (pickerLabel == nil) {
        CGRect frame = CGRectMake(0.0, 0.0, 50, 60);
        pickerLabel = [[UILabel alloc] initWithFrame:frame];
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:15.0f]];
    }
    
    if (component == 0)
    {
        pickerLabel.text =  [yearArray objectAtIndex:row]; // Year
    }
    else if (component == 1)
    {
        pickerLabel.text =  [monthArray objectAtIndex:row];  // Month
    }
    else if (component == 2)
    {
        pickerLabel.text =  [DaysArray objectAtIndex:row]; // Date
        
    }
    return pickerLabel;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    if (component == 0)
    {
        return [yearArray count];
    }
    else if (component == 1)
    {
        NSInteger selectRow =  [pickerView selectedRowInComponent:0];
        int n;
        n= year-1970;
        if (selectRow==n) {
            return [monthMutableArray count];
        }else
        {
            return [monthArray count];
            
        }
    }
    else
    {
        NSInteger selectRow1 =  [pickerView selectedRowInComponent:0];
        int n;
        n= year-1970;
        NSInteger selectRow =  [pickerView selectedRowInComponent:1];
        
        if (selectRow==month-1 &selectRow1==n) {
            
            return day;
            
        }else{
            
            if (selectedMonthRow == 0 || selectedMonthRow == 2 || selectedMonthRow == 4 || selectedMonthRow == 6 || selectedMonthRow == 7 || selectedMonthRow == 9 || selectedMonthRow == 11)
            {
                return 31;
            }
            else if (selectedMonthRow == 1)
            {
                int yearint = [[yearArray objectAtIndex:selectedYearRow]intValue ];
                
                if(((yearint %4==0)&&(yearint %100!=0))||(yearint %400==0)){
                    return 29;
                }
                else
                {
                    return 28; // or return 29
                }
            }
            else
            {
                return 30;
            }
        }
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
@end
